//===============================================================================
// Microsoft patterns & practices Enterprise Library Contribution
// Query Application Block
//===============================================================================

using System;
using System.Configuration;
using System.Linq.Expressions;
using Microsoft.Practices.EnterpriseLibrary.Common.Configuration.ContainerModel;
using EntLibContrib.Query.Queries;
using Microsoft.Practices.EnterpriseLibrary.Common.Configuration.Design;

namespace EntLibContrib.Query.Configuration
{
	/// <summary>
	/// Configuration data defining XmlQueryData. This configuration section adds the element
	/// </summary>    
    [ResourceDescription(typeof(DesignResources), "XmlFileQueryDataDescription")]
    [ResourceDisplayName(typeof(DesignResources), "XmlFileQueryDataDisplayName")]
	public class XmlFileQueryData : FileQueryData
	{
		private const string elementProperty = "element";

		/// <summary>
		/// Initializes a new instance of the <see cref="XmlFileQueryData"/> class.
		/// </summary>
		public XmlFileQueryData ()
            : base(null, typeof(XmlFileQuery))
		{
		}

		/// <summary>
		/// Initialize a new instance of the <see cref="XmlFileQueryData"/> class.
		/// </summary>
		/// <param name="name">The name of the <see cref="XmlFileQueryData"/>.</param>
		/// <param name="customCommandName">Name of the custom command.</param>
		/// <param name="parameterSetName">Name of the parameter set.</param>
		/// <param name="dataFile">The data file.</param>
		/// <param name="element">The element in the data file that holds a data object.</param>
		public XmlFileQueryData(string name, string customCommandName, string parameterSetName, string dataFile, string element)
			: base(name, typeof(XmlFileQuery), customCommandName, parameterSetName, dataFile)
		{
			this.Element = element;
		}

		/// <summary>
		/// The xml element to use.
		/// </summary>
		/// <value>The xml element.</value>
		[ConfigurationProperty(elementProperty, IsRequired = false)]
        [ResourceDescription(typeof(DesignResources), "XmlFileQueryDataElementDescription")]
        [ResourceDisplayName(typeof(DesignResources), "XmlFileQueryDataElementDisplayName")]
		public string Element
		{
			get { return (string)base[elementProperty]; }
			set { base[elementProperty] = value; }
		}

        /// <summary>
        /// Returns the <see cref="TypeRegistration"/> entry for the current concrete type represented by this 
        /// configuration object.
        /// </summary>
        /// <returns>A registry entry.</returns>
		protected override Expression<Func<IQuery>> GetQueryCreationExpression()
		{
			return () =>
			       new XmlFileQuery(
                       Name,
			           Element,
			           DataFile,
                       Container.ResolvedIfNotNull<ICommand>(CustomCommandName),
                       Container.ResolvedIfNotNull<IParameterSet>(ParameterSetName));
		}
	}
}